#include "encoding.h"

.section .init
.global _start
.type _start,@function

_start:
    /* Disable Global Interrupt */
    csrc CSR_MSTATUS, MSTATUS_MIE
    /* Disable all interrupts controlled by CSR MIE */
    csrw CSR_MIE, 0x0

    /* Initialize GP and Stack Pointer SP */
    .option push
    .option norelax
    la gp, __global_pointer$
    .option pop
    la sp, STACK


    /* Set Trap Entry MTVEC to _vector_table */
    la t0, _vector_table+1
    csrw mtvec, t0
#ifdef PLIC_VEC
    la t0, _ext_vector_table +1
    csrw 0x7d4, t0
#endif

    /* Enable FPU */
    li t0, MSTATUS_FS
    csrs mstatus, t0

#ifdef BOOT_EFLASH
    /* Load data section from flash to sram */
    la a0, _sidata
    la a1, _sdata
    la a2, _edata
    bgeu a1, a2, 2f
    /*Copy data setcion*/
1:
    lw t0, (a0)
    sw t0, (a1)
    addi a0, a0, 4
    addi a1, a1, 4
    bltu a1, a2, 1b
    /*Copy data setcion done*/
2:
#endif

    /* Clear bss section */
    la t0, _bss_start
    la t1, _bss_end
    bgeu a0, a1, 2f
1:
    sw   zero,0(t0)
    addi t0, t0, 4
    bltu t0, t1, 1b

2:

    call print_init
    call main

_finish:
1:
    j 1b